<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsPurchaseInfo extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $model = new \app\api\model\kuyou\applet\GoodsPurchaseInfo;

            $where = [];$map = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['th.purchase_code'] = ['like','%'.$code.'%'];
                $map['purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['th.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                    $map['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $supplyer_code = $this->request->post('supplyer_code');
            if(!empty($supplyer_code)){
                $where['th.supplyer_code'] = $supplyer_code;
                $map['supplyer_code'] = $supplyer_code;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['th.goods_code'] = $goods_code;
                $map['goods_code'] = $goods_code;
            }
            $keyword = $this->request->post('keyword');
            if(!empty($keyword)){
                $where['th.purchase_code|th.supplyer_code|th.goods_code'] = $keyword;
                $map['purchase_code|supplyer_code|goods_code'] = $keyword;
            }
            $where['th.examine_status'] = '1';
            $map['examine_status'] = '1';

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }
            if($pageStatus){
                $list = Db::name('kuyou_goods_purchase_info')->alias('th')
                    ->join('kuyou_goods g','g.goods_code=th.goods_code')
                    ->join('kuyou_unit u','u.id=th.goods_unit')
                    ->join('kuyou_suppliers s','s.supplyer_code=th.supplyer_code')
                    ->join('kuyou_stores st','st.id=th.stores_id')
                    ->where($where)
                    ->field('g.goods_name,g.goods_image,g.goods_specification_id as goods_spec,g.goods_code,g.goods_barcode,
                        u.name as unit_name,s.supplyer_code,s.supplyer_name,st.name as stores_name,
                        FROM_UNIXTIME(th.purchase_time,\'%Y-%m-%d\') as purchase_time,th.purchase_code,
                        th.purchase_type,th.number,th.unit_price,th.tax_included,th.all_money,th.purchase_money,
                        th.tax_money,th.money_tax_price,th.purchase_order_id,th.remark')
                    ->page($page,$limit)
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_purchase_info')->alias('th')
                    ->join('kuyou_goods g','g.goods_code=th.goods_code')
                    ->join('kuyou_unit u','u.id=th.goods_unit')
                    ->join('kuyou_suppliers s','s.supplyer_code=th.supplyer_code')
                    ->join('kuyou_stores st','st.id=th.stores_id')
                    ->where($where)
                    ->field('g.goods_name,g.goods_image,g.goods_specification_id as goods_spec,g.goods_code,g.goods_barcode,
                        u.name as unit_name,s.supplyer_code,s.supplyer_name,st.name as stores_name,
                        FROM_UNIXTIME(th.purchase_time,\'%Y-%m-%d\') as purchase_time,th.purchase_code,
                        th.purchase_type,th.number,th.unit_price,th.tax_included,th.all_money,th.purchase_money,
                        th.tax_money,th.money_tax_price,th.purchase_order_id,th.remark')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_purchase_info')->alias('th')
                ->join('kuyou_goods g','g.goods_code=th.goods_code')
                ->join('kuyou_unit u','u.id=th.goods_unit')
                ->join('kuyou_suppliers s','s.supplyer_code=th.supplyer_code')
                ->join('kuyou_stores st','st.id=th.stores_id')
                ->where($where)->count();

            $price_decimal = \think\Db::name('kuyou_sets')->where(['name'=>'price_decimal'])->value('value');
            $number_decimal = \think\Db::name('kuyou_sets')->where(['name'=>'number_decimal'])->value('value');

            foreach ($list as $list_k=>$list_v){
                if($list_v['purchase_type']==1){
                    $note = Db::name('kuyou_goods_purchase')->where(['purchase_code'=>$list_v['purchase_code']])->value('note');
                }else{
                    $note = Db::name('kuyou_goods_exchange')->where(['purchase_code'=>$list_v['purchase_code']])->value('note');
                    $list[$list_k]['number'] = -$list_v['number'];
                    $list[$list_k]['all_money'] = -$list_v['all_money'];
                    $list[$list_k]['tax_money'] = -$list_v['tax_money'];
                    $list[$list_k]['money_tax_price'] = -$list_v['money_tax_price'];
                }
                $list[$list_k]['note'] = $note;

                $list[$list_k]['number'] = round($list[$list_k]['number'],$number_decimal);
                $list[$list_k]['unit_price'] = round($list[$list_k]['unit_price'],$price_decimal);
                $list[$list_k]['tax_included'] = round($list[$list_k]['tax_included'],$price_decimal);
                $list[$list_k]['all_money'] = round($list[$list_k]['all_money'],$price_decimal);
                $list[$list_k]['purchase_money'] = round($list[$list_k]['purchase_money'],$price_decimal);
                $list[$list_k]['tax_money'] = round($list[$list_k]['tax_money'],$price_decimal);
                $list[$list_k]['money_tax_price'] = round($list[$list_k]['money_tax_price'],$price_decimal);
            }

            $number = $model->where($map)->sum('number');
            $all_money = $model->where($map)->sum('all_money');
            $purchase_money = $model->where($map)->sum('purchase_money');
            $tax_money = $model->where($map)->sum('tax_money');
            $money_tax_price = $model->where($map)->sum('money_tax_price');

            if($number>0){
                $unit_price = round($all_money/$number,$price_decimal);
                $tax_included = round($money_tax_price/$number,$price_decimal);
            }else{
                $unit_price = 0;
                $tax_included = 0;
            }
            $number = round($number,$number_decimal);
            $all_money = round($all_money,$price_decimal);
            $purchase_money = round($purchase_money,$price_decimal);
            $tax_money = round($tax_money,$price_decimal);
            $money_tax_price = round($money_tax_price,$price_decimal);
            $count = ['number'=>$number,'unit_price'=>$unit_price,'tax_included'=>$tax_included,'all_money'=>$all_money,'purchase_money'=>$purchase_money,'tax_money'=>$tax_money,'money_tax_price'=>$money_tax_price];

            $result = array("count" => $row_count, "list" => $list,'sum'=>$count);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
